package com.Lyoto.NetNotify.Config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.Lyoto.NetNotify.Utils.YmlUtil;
import lombok.extern.slf4j.Slf4j;

/***
 @author Lyoto
 @description
 @create 2024/3/17-16:35
 ***/
@Slf4j
public class EmailClient {
    private MailAccount mailAccount;

    private String to;

    public EmailClient() {
        String e_mail = YmlUtil.prase("mail.email-name");
        String name = e_mail.substring(0,e_mail.indexOf("@"));
        String secret = YmlUtil.prase("mail.secret");
        String address = YmlUtil.prase("mail.server.address");
        this.to = YmlUtil.prase("mail.to");
        mailAccount = new MailAccount();
        mailAccount.setAuth(true);
        mailAccount.setSslEnable(true);
        mailAccount.setHost(address);
        mailAccount.setUser(name);
        mailAccount.setFrom(e_mail);
        mailAccount.setPass(secret);
    }
    public void sendNotifyEmail(String ip){
        String msgId = MailUtil.send(this.mailAccount, CollUtil.newArrayList(to), "ipNotify", ip, false);
        log.info("发送邮件成功 msgId {}",msgId);
    }
}
